﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using MVCFoto.BLL;
using MVCFoto.BLL.Graphic;
using System.Drawing;
using System.Drawing.Imaging;
namespace MVCFoto.Web.Utility
{
    public class Upload
    {
        public static string UploadAvatar(HttpPostedFileBase hpf,int userId)
        {
            string ext = Path.GetExtension(hpf.FileName).ToLower();

           
            string filename = String.Format("{0}{1}", userId,ext);

            string path = Path.Combine(MVCFotoSettings.UserAvatarDirectoy, filename);
            //if (ext != ".jpg")
            //{
            //    //非jpg图像另存为jpg
            //    Image img = Image.FromStream(hpf.InputStream);
            //    //处理JPG质量的函数
            //    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            //    ImageCodecInfo ici = null;
            //    foreach (ImageCodecInfo codec in codecs)
            //    {
            //        if (codec.MimeType == "image/jpeg")
            //            ici = codec;
            //    }
            //    EncoderParameters ep = new EncoderParameters();
            //    ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95);
            //    img.Save(path, ici, ep);
            //    img.Dispose();
                
            //}
            //else
                hpf.SaveAs(path);
            Thumbnail.ThumbnailAvatar(path);
            return MVCFoto.BLL.MVCFotoSettings.UserAvatarUrl + String.Format("{0}.jpg", userId); ;
        }
    }
}
